<?php
/**
 * 栏目分类的类库
 */

namespace app\Http\Org;


class Categoryclass
{
    /**
     * @param $data        laravel all方法生成的全部数组
     * @param string $html 展示字符串
     * @param int $pid     栏目的父id
     * @param $level       //栏目的等级
     * @return array       返回二维数组，用于后台栏目的列表展示 ----方法1 递归排序----
     */
    public static function catesort($data, $html='|--', $pid = 0, $level = 0)
    {
        $arr = [];
        foreach ($data as $v) {
            if($v->cate_pid == $pid) {
                $v->level = $level + 1;
                $v->html = str_repeat($html, $level);
                $arr[] = $v;

                $arr = array_merge($arr, self::catesort($data, $html, $v->cate_id, $level+1));
            }
        }
        return $arr;
    }

    /**
     * @param $data
     * @param string $html
     * @param int $pid
     * @param int $level
     * @return array  返回二维数组，用于后台栏目的列表展示 ----方法2 遍历排序(只用于两层分类)----
     */
    public static function cate_foreach($data, $html='|--', $pid = 0, $level = 0)
    {
        $arr = [];
        foreach ($data as $v) {
            if($v->cate_pid == $pid) {
                $v->level = $level + 1;
                $v->html = str_repeat($html, $level);
                $arr[] = $v;

                foreach ($data as $m=>$n) {
                    if($n->cate_pid == $v->cate_id) {
                        $n->level = 2;
                        $n->html = '|-- ';
                        $arr[] = $n;
                    }
                }
            }
        }
        return $arr;
    }

    //返回一个多维数组：用于首页导航二级导航
    Static Public function catesorts($cate,$name='child',$pid=0){
        $arr = array();
        foreach($cate as $v){
            if($v['pid'] == $pid){
                $v[$name] = self::catesorts($cate,$name,$v['id']);
                $arr[] = $v;
            }
        }
        return $arr;
    }

    //提供子ID返回父级栏目 用于：面包屑导航
    Static Public function getParents($cate,$id){
        $arr = array();
        foreach($cate as $v){
            if($v['id'] == $id){
                $arr[] = $v;
                $arr = array_merge(self::getParents($cate,$v['pid']),$arr);
            }
        }
        return $arr;
    }

    //提供父级ID返回所有子栏目ID
    Static Public function getSonId($cate,$pid){
        $arr = array();
        foreach($cate as $v){
            if($v['pid'] == $pid){
                $arr[] = $v['id'];
                $arr = array_merge($arr,self::getSonId($cate,$v['id']));
            }
        }
        return $arr;
    }

    //提供父级ID返回所以子栏目二维数组
    Static Public function getSon($cate,$pid){
        $arr = array();
        foreach($cate as $v){
            if($v['pid'] == $pid){
                $arr[] = $v;
                $arr = array_merge($arr,self::getSon($cate,$v['id']));
            }
        }
        return $arr;
    }
    //提供父级ID返回所以子栏目多维数组
    Static Public function getSons($cate,$name='child',$pid){
        $arr = array();
        foreach($cate as $v){
            if($v['pid'] == $pid){
                $v[$name] = self::getSons($cate,$name,$v['id']);
                $arr[] = $v;
            }
        }
        return $arr;
    }
}